package cn.sciento.iam.api.controller.v1;

import cn.sciento.core.base.BaseConstants;
import cn.sciento.core.iam.ResourceLevel;
import cn.sciento.core.oauth.DetailsHelper;
import cn.sciento.core.util.Results;
import cn.sciento.iam.api.dto.SubModuleDTO;
import cn.sciento.iam.app.service.SubModuleService;
import cn.sciento.iam.config.SwaggerApiConfig;
import cn.sciento.iam.domain.entity.SubModule;
import cn.sciento.iam.domain.repository.SubModuleRepository;
import cn.sciento.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Validator;
import java.util.List;

@Api(tags = SwaggerApiConfig.IAM_SUB_MODULE)
@RestController("subModuleController.v1")
@RequestMapping("/v1/{tenantId}/sub-module")
public class SubModuleController {

    @Autowired
    private SubModuleService subModuleService;

    @Autowired
    private SubModuleRepository subModuleRepository;
    @Autowired
    private Validator validator;

    @ApiOperation("子模块-获取租户的所有模块")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping(value = "")
    public ResponseEntity<List<SubModule>> list(@PathVariable Long tenantId, SubModuleDTO subModuleDTO){
        subModuleDTO.setTenantId(tenantId);
        return Results.success(subModuleService.listSubModule(subModuleDTO));
    }

    @ApiOperation("子模块-获取用户的模块")
    @Permission(level = ResourceLevel.ORGANIZATION,permissionLogin = true)
    @GetMapping(value = "self")
    public ResponseEntity getSelfModule(@PathVariable Long tenantId){
        List<SubModule> subModules = subModuleService.getUserModule(DetailsHelper.getUserDetails().getUserId(),tenantId);
        return Results.success(subModules);

    }


}
